﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework;
using System.IO;
using System.Windows.Forms;
using Microsoft.Xna.Framework.Content;
using Debug;

namespace Kristall.Managers
{
    public class Sound2D
    {
        public Cue Cue
        {
            get;
            set;
        }

        public static float SoundDepth
        {
            get
            {
                return Kristall.Options.sound_depth;
            }        
        }

        AudioListener listener = new AudioListener();
        AudioEmitter emitter = new AudioEmitter();

        string _cue;

        public Sound2D(string cue)
        {
            _cue = cue;
        }

        public void Play()
        {
            Cue = Kristall.SoundManager.SoundBank.GetCue(_cue);
            Cue.Apply3D(listener, emitter);
            //if(!Cue.IsPlaying)
            Cue.Play();
        }

        public void Update(Vector2 listenerPosition, Vector2 emitterPosition)
        {
            if (Cue == null)
                return;

            listener.Position = new Vector3(listenerPosition, 0);
            emitter.Position = new Vector3(emitterPosition, Sound2D.SoundDepth);

            if(Cue.IsPlaying)
                Cue.Apply3D(listener, emitter);
        }
   }

    public class SoundManager : GameComponent
    {
        #region properties

        public AudioEngine Engine { get; set; }

        public SoundBank SoundBank { get; set; }

        public WaveBank WaveBank { get; set; }

        public float Volume { get { return Kristall.Options.sound_volume; } }

        #endregion  

        AudioCategory category;
        
        public SoundManager(Game game) : base(game)
        {
            Engine = new AudioEngine(@"Content\Sounds\XACTProject.xgs");
            SoundBank = new SoundBank(Engine, @"Content\Sounds\Sound Bank.xsb");
            WaveBank = new WaveBank(Engine, @"Content\Sounds\Wave Bank.xwb");
        }

        public void SetCategoryVolume(float MusicVolume, string Category)
        {
            category = Engine.GetCategory(Category);
            category.SetVolume(MusicVolume);
        }

        public void PlayNonInstancedSound(string _cue)
        {
            Cue cue = SoundBank.GetCue(_cue);
            if (cue != null)
                cue.Play();
        }

        public void StopAll(string Category)
        {
            category = Engine.GetCategory(Category);
            category.Stop(AudioStopOptions.Immediate);            
        }

        public void PauseAll(string Category)
        {
            category = Engine.GetCategory(Category);
            category.Pause();
        }

        public void ResumeAll(string Category)
        {
            category = Engine.GetCategory(Category);
            category.Resume();
        }

        public override void  Update(GameTime gameTime)
        {
            Engine.Update(); 
            base.Update(gameTime);
        }
    }
}
